package com.iflysse.dao;

import com.iflysse.pojo.po.req.CourseIdPoReq;
import com.iflysse.pojo.po.req.CoursePoReq;
import com.iflysse.pojo.po.resp.CourseDetailPoResp;
import com.iflysse.pojo.po.resp.CoursePoResp;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

@Repository
public interface CourseDao {
    /**
     * 根据老师编号查询老师创建的课程列表
     * @param teacherId 老师编号
     * @return
     */
    @Select({"select * from tb_course where teacher_id = #{teacherId} and is_delete = 0"})
    @Results({
            @Result(property = "courseId",column = "course_id"),
            @Result(property = "teacherId",column = "teacher_id"),
            @Result(property = "courseCover",column = "course_cover"),
            @Result(property = "courseName",column = "course_name"),
            @Result(property = "updateTime",column = "update_time"),
            @Result(property = "charpterAmount",column = "charpter_amount"),
    })
    List<CoursePoResp> getAllCoursesByTeacherId(String teacherId) throws SQLException;

    /**
     * 根据老师编号查询老师创建的最新6门课程
     * @param teacherId
     * @return
     */
    @Select({"select * from tb_course where teacher_id = #{teacherId} and is_delete = 0  order by update_time desc limit 0,6"})
    @Results({
            @Result(property = "courseId",column = "course_id"),
            @Result(property = "teacherId",column = "teacher_id"),
            @Result(property = "courseCover",column = "course_cover"),
            @Result(property = "courseName",column = "course_name"),
            @Result(property = "updateTime",column = "update_time"),
            @Result(property = "charpterAmount",column = "charpter_amount"),
    })
    List<CoursePoResp> getTop6CourseByTeacherId(String teacherId) throws SQLException;


    /**
     * 添加课程
     * @param coursePoReq 课程
     * @return 受影响行数
     */
    @Insert({"insert into tb_course(course_id,teacher_id,course_cover,course_name,update_time) values(#{courseId},#{teacherId},#{courseCover},#{courseName},now())"})
    @Options(useGeneratedKeys = true, keyProperty = "chapter_id")
    int addCourse(CoursePoReq coursePoReq) throws SQLException;


    /**
     * 添加课程标签(批量)
     * @param coursePoReq 课程请求实体
     * @return
     */
    @Insert({
            "<script>",
            "insert into tb_course_label (course_label_id,course_id,label_id) values "+
            "<foreach collection='labelIdList' item='item' index='index' separator=','>"+
            "(uuid(), #{courseId}, #{item}) "+
            "</foreach>"+
            "</script>"
    })
    @Options(useGeneratedKeys = true, keyProperty = "course_label_id")
    int addCourseLabel(CoursePoReq coursePoReq) throws SQLException;


    /**
     * 删除课程对应的标签
     *
     * @param courseId 课程编号
     * @return
     */
    @Delete({"delete from tb_course_label where course_id = #{courseId}"})
    int deleteCourseLabelByCourseId(String courseId) throws SQLException;

    /**
     * 修改课程信息
     *
     * @param coursePoReq 课程请求实体
     * @return
     */
    @Update("update tb_course set  course_name = #{courseName} ,course_cover = #{courseCover} where course_id = #{courseId}")
    int modifyCourse(CoursePoReq coursePoReq) throws SQLException;


    /**
     * 根据课程编号获取课程详情
     * @param courseIdPoReq
     * @return
     */
    @Select({"select * from tb_course where course_id = #{courseId}"})
    @Results({
            @Result(property = "courseId",column = "course_id"),
            @Result(property = "courseCover",column = "course_cover"),
            @Result(property = "courseName",column = "course_name"),
            @Result(property = "chapterAmount",column = "charpter_amount"),
    })
    CourseDetailPoResp getDetailByCourseId(CourseIdPoReq courseIdPoReq) throws SQLException;

    /**
     * 根据课程编号获取课程封面路径
     * @param courseIdPoReq
     * @return
     */
    @Select({"select course_cover from tb_course where course_id = #{courseId}"})
    @Results({
            @Result(property = "courseCover",column = "course_cover"),
    })
    String getCourseCoverByCourseId(CourseIdPoReq courseIdPoReq) throws SQLException;

    /**
     * 更新课程的章数量
     * @param courseIdPoReq
     * @param amount
     * @return
     */
    @Update("update tb_course set  charpter_amount = #{amount} where course_id = #{courseIdPoReq.courseId}")
    int updateChapterAmountByCourseId(@Param("courseIdPoReq") CourseIdPoReq courseIdPoReq, int amount) throws SQLException;

    /**
     * 根据课程编号获取该课程的发布状态,0是未发布,1是已发布
     * @param courseIdPoReq
     * @return
     */
    @Select({"select is_publish from tb_source where course_id = #{courseId}"})
    int getPublishStateByCourseId(CourseIdPoReq courseIdPoReq) throws SQLException;

}
